package com.qianfeng.shopping.dao;

import com.qianfeng.shopping.entity.Goods;
import com.qianfeng.shopping.entity.Kind;

public class GoodsDao {
	static Goods[] arr = new Goods[10];
	//  初始化数据
	static {
		arr[0] = new Goods("g001", "iphone14", 6000, 0.25, "2023-1-10 13:38:59",  new Kind(1,"电子产品",""));
		arr[1] = new Goods("g002", "mi13", 5000, 0.22, "2022-11-10 13:38:59",  new Kind(1,"电子产品",""));
		arr[2] = new Goods("g003", "iphone13", 4000, 0.27, "2022-11-11 13:38:59",  new Kind(1,"电子产品",""));
		arr[3] = new Goods("g004", "衬衫", 300, 0.68, "2022-11-08",  new Kind(2,"服装",""));
		arr[4] = new Goods("g005", "领带", 200, 0.28, "2022-11-08",  new Kind(2,"服装",""));
	}
	// 一览数据
	public Goods[] selectAll() {
		return arr;
	}
	// 查询数据
	public Goods[] select(String key) {
		int len = 0;
		for (int i = 0; i < arr.length; i++) {
			if(arr[i]!=null&&arr[i].getGoodsName().contains(key)) {
				len++;
			}
		}
		Goods[] array = new Goods[len];
		int index = 0;
		for (int i = 0; i < arr.length; i++) {
			if(arr[i]!=null&&arr[i].getGoodsName().contains(key)) {
				array[index] = arr[i];
				index++;
			}
		}
		return array;
	}
	// 删除
	public boolean delete(String goodsId) {
		for (int i = 0; i < arr.length; i++) {
			if(arr[i]!=null&&arr[i].getGoodsId().equals(goodsId)) {
				arr[i] = null;
				return true;
			}
		}
		return false;
	}
	// 利用id查询是否存在
	public boolean checkGoodsId(String goodsId) {
		for (int i = 0; i < arr.length; i++) {
			if(arr[i]!=null&&arr[i].getGoodsId().equals(goodsId)) {
				return true;
			}
		}
		return false;
	}
	public Goods selectByGoodsId(String goodsId) {
		for (int i = 0; i < arr.length; i++) {
			if(arr[i]!=null&&arr[i].getGoodsId().equals(goodsId)) {
				return arr[i];
			}
		}
		return null;
	}
	// 新建
	public boolean insert(Goods goods) {
		for (int i = 0; i < arr.length; i++) {
			if (arr[i]==null) {
				arr[i] = goods;
				return true;
			}
		}
		return false;
	}
	// 修改
	public boolean update(Goods goods) {
		for (int i = 0; i < arr.length; i++) {
			if (arr[i]!=null&&arr[i].getGoodsId().equals(goods.getGoodsId())) {
				arr[i].setGoodsName(goods.getGoodsName());
				arr[i].setGoodsPrice(goods.getGoodsPrice());
				arr[i].setGoodsWeight(goods.getGoodsWeight());
				arr[i].setGoodsTime(goods.getGoodsTime());
				return true;
			}
		}
		return false;
	}
}
